package com.victor.modular.system.service.impl;

import com.victor.common.constant.Const;
import com.victor.common.constant.state.ManagerStatus;
import com.victor.common.exception.BizExceptionEnum;
import com.victor.common.exception.BussinessException;
import com.victor.common.persistence.model.User;
import com.victor.core.shiro.ShiroKit;
import com.victor.core.shiro.ShiroUser;
import com.victor.modular.system.service.BaseService;
import com.victor.modular.system.service.IUserService;
import com.victor.modular.system.transfer.UserDto;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 用户管理Service
 *
 * @author zhaojiabin
 * @Date 2017-09-04 12:07:30
 */
@Service
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class UserServiceImpl extends BaseService implements IUserService {
    /**
     * 添加用户
     */
    @Override
    public void add(UserDto userDto) throws Exception {
        // 判断账号是否重复
        User theUser = userMgrDao.getByAccount(userDto.getAccount());
        if (theUser != null) {
            throw new BussinessException(BizExceptionEnum.USER_ALREADY_REG);
        }
        User user = new User();
        BeanUtils.copyProperties(userDto, user);
        // 完善账号信息
        user.setClearpwd(user.getPassword());
        user.setSalt(ShiroKit.getRandomSalt(5));
        user.setPassword(ShiroKit.md5(user.getPassword(), user.getSalt()));
        user.setStatus(ManagerStatus.OK.getCode());
        user.setCreatetime(new Date());
        user.setCreateUserId(ShiroKit.getUser().getId());
        this.userMapper.insert(user);
        //删除缓存
        clearAllCache();
    }

    /**
     * 修改用户
     */
    @Override
    public void update(UserDto userDto) throws Exception {
        boolean update = false;
        if (ShiroKit.hasRole(Const.ADMINISTRATOR_NAME) || ShiroKit.hasRole(Const.ADMIN_NAME)) {
            update = true;
        } else {
            ShiroUser shiroUser = ShiroKit.getUser();
            if (shiroUser.getId().equals(userDto.getId())) {
                update = true;
            } else {
                throw new BussinessException(BizExceptionEnum.NO_PERMITION);
            }
        }
        if (update) {
            User user = this.userMapper.selectById(userDto.getId());
            user.setUpdatetime(new Date());
            user.setUpdateUserId(ShiroKit.getUser().getId());
            user.setAccount(userDto.getAccount());
            user.setName(userDto.getName());
            user.setBirthday(userDto.getBirthday());
            user.setSex(userDto.getSex());
            user.setEmail(userDto.getEmail());
            user.setPhone(userDto.getPhone());
            user.setRoleid(userDto.getRoleid());
            user.setStatus(userDto.getStatus());
            user.setAvatarUrl(userDto.getAvatarUrl());
            this.userMapper.updateById(user);
        }
        //删除缓存
        clearAllCache();

    }


    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Integer id) throws Exception {
        User user = userMapper.selectById(id);
        user.setStatus(ManagerStatus.DELETED.getCode());
        userMapper.updateById(user);
        //删除缓存
        clearAllCache();
    }


}
